package com.ppt.javappt.entity;

import com.ppt.javappt.em.CssPathEnum;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;

import java.util.List;

/**
 * @author cys
 * 左边导航栏
 */
public class ThumbnailPane extends ListView<Slide> {
    ObservableList<Slide> imageViewList = FXCollections.observableArrayList();
    public ThumbnailPane(){
        this.getStyleClass().add("thumbnail-pane");

        this.setPrefHeight(620);
        this.setPrefWidth(230);
        this.setItems(imageViewList);
        // 加载样式表
        this.getStylesheets().add(CssPathEnum.THUMBNAIL_PANE_CSS.getUrl());

        this.setCellFactory(param -> new ListCell<>() {
            // 用于显示缩略图的 ImageView
            private ImageView thumbnailView = new ImageView();
            // 1. 定义原始监听器（业务逻辑）
            private final ChangeListener<WritableImage> originalListener = (obs, oldImg, newImg) -> {
                // 缩略图变化时刷新单元格
                updateItem(getItem(), isEmpty());
            };
            // 2. 用 WeakChangeListener 包装原始监听器（弱引用，避免内存泄漏）
            private final WeakChangeListener<WritableImage> thumbnailListener =
                    new WeakChangeListener<>(originalListener);
//            private final ChangeListener<WritableImage> thumbnailListener = (obs, oldImg, newImg) -> {
//                // 缩略图变化时，强制刷新当前单元格
//                updateItem(getItem(), isEmpty());
//            };
            @Override
            protected void updateItem(Slide slide, boolean empty) {

                // 获取当前单元格之前绑定的对象
                Slide oldSlide = getItem();
                if (oldSlide != null) {
                    oldSlide.thumbnailProperty().removeListener(thumbnailListener);
                }
                super.updateItem(slide, empty);
                // 1. 处理空单元格或数据为空的情况
                if (empty || slide == null) {
                    setGraphic(null);
                }else{
                    thumbnailView.setFitWidth(200);
                    thumbnailView.setFitHeight(150);
                    thumbnailView.setPreserveRatio(true);

                    thumbnailView.setImage(slide.getThumbnail());
                    setGraphic(thumbnailView);

                    slide.thumbnailProperty().addListener(thumbnailListener);
                }
            }
        });
    }
    public void setSlideList(List<Slide> list){
        imageViewList.clear();
        imageViewList.addAll(list);
        this.setItems(imageViewList);
        System.out.println(imageViewList.size());
    }
//    private ImageView createThumbnail(Slide slide){
//        ImageView thumbnail = new ImageView(slide.getThumbnail());
//        thumbnail.setPreserveRatio(true);
//        return thumbnail;
//    }
//    public void addImageView(Slide slide){
//        imageViewList.add(slide);
//    }
//    public void updateImageView(){
//        setSlideList(SystemUtil.getInstance().getCurSlideFile().slideList);
//    }
}
